package com.lianniu.mgr.supplyManage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.baseService.dict.model.DictionaryVo;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.supplyManage.service.ISupplyAreaService;
import com.lianniu.mgr.supplyManage.service.ISupplyCommissionService;
import com.lianniu.mgr.supplyManage.service.ISupplyManageService;
import com.lianniu.mgr.supplyManage.vo.SupplyAreaVo;
import com.lianniu.mgr.supplyManage.vo.SupplyCommissionVo;
import com.lianniu.mgr.supplyManage.vo.SupplyConditionVo;
import com.lianniu.mgr.supplyManage.vo.SupplyVo;
import com.lianniu.mgr.util.FileUploadUtil;
import com.lianniu.mgr.util.ObjectUtil;

@Controller
@RequestMapping("/rest/supplyManage")
public class SupplyManageRestController {
  
  private static Logger logger = LoggerFactory.getLogger(SupplyManageRestController.class);
  @Autowired
  ISupplyManageService supplyManageService;
  @Autowired
  FileUploadUtil fileUpload;
  @Autowired
  IAdminAreaService areaService;    
  @Autowired
  IDictionaryService dictionaryService;
  @Autowired
  ISupplyCommissionService commissionService;
  @Autowired
  ISupplyAreaService supplyAreaService;
  

  @RequestMapping("/supplyList")
  public @ResponseBody BaseResult supplyList(SupplyConditionVo condition,
      PaginationInfo paginationInfo, HttpServletRequest request, HttpServletResponse response) {
    logger.info(String.format("query supplylist,params:condition=[%s],page=[%s]", 
        JSON.toJSONString(condition),JSON.toJSONString(paginationInfo)));
    BaseResult br = supplyManageService.getSupplyListForPage(condition, paginationInfo);
    logger.info(String.format("query supplylist result:%s", JSON.toJSONString(br)));
    return br;
  }
  /**
   * 
  * @author guoconglin: 
  * @Description: 查询供应商名称
  * @date 创建时间：2016年8月5日 下午2:24:40 
  * @version 1.0 
  * @parameter  
  * @since  
  * @return
   */
  @RequestMapping("/supplyListNoPage")
  public @ResponseBody BaseResult supplyListNoPage(HttpServletRequest request,ModelMap map) {
    logger.info("query supplylist,params:condition=[%s],page=[%s]"+ 
        JSON.toJSONString(""));
    BaseResult br = supplyManageService.getSupplyNameList();
    logger.info(String.format("query supplylist result:%s", JSON.toJSONString("")));
    return br;
  }
  @RequestMapping("/getSupply")
  public @ResponseBody BaseResult getSupply(HttpServletRequest request) {
    String supplyid = request.getParameter("supplyid");
    logger.info(String.format("get %s supply", supplyid));
    BaseResult supply = supplyManageService.getSupplyById(supplyid);
    SupplyCommissionVo commission = (SupplyCommissionVo)commissionService.getCommission(((SupplyVo)supply.getData()).getSupplyID()).getData();
    ((SupplyVo)supply.getData()).setCommission(commission);
    logger.info("get supply result : "+JSON.toJSONString(supply));
    return supply;
  }
  
  @SuppressWarnings("unchecked")
  @RequestMapping("/getSupplyWithArea")
  public @ResponseBody BaseResult getSupplyWithArea(HttpServletRequest request,ModelMap map) {
    String supplyid = request.getParameter("supplyid");
    logger.info(String.format("get %s supply", supplyid));
    BaseResult br = supplyManageService.getSupplyById(supplyid);
    
    SupplyVo supply = (SupplyVo) br.getData();
    ObjectUtil.nvlProperty(supply);//将null转换为空字符串
    List<AdminAreaVo> provinces =( List<AdminAreaVo>) areaService.getByParentId("1").getData();
    List<AdminAreaVo> citys = ( List<AdminAreaVo>) areaService.getByParentId(supply.getProvince()).getData();
    List<AdminAreaVo> countrys = (List<AdminAreaVo>) areaService.getByParentId(supply.getCity()).getData();

    DictionaryVo dic = new DictionaryVo();
    dic.setTypeName("pedigreeYear");
    BaseResult pedigreeYearType= dictionaryService.selectDictionaryByDicType(dic);
    
    dic.setTypeName("scale");
    BaseResult scaleTypeType = dictionaryService.selectDictionaryByDicType(dic);

    dic.setTypeName("avgYield");
    BaseResult avgYieldType = dictionaryService.selectDictionaryByDicType(dic);

    map.put("provinces", provinces);
    map.put("citys", citys);
    map.put("countrys", countrys);
    map.put("supply", supply);

    map.put("pedigreeYearType", pedigreeYearType.getData());
    map.put("scaleType", scaleTypeType.getData());
    map.put("avgYieldType", avgYieldType.getData());


    logger.info("get supply result : "+JSON.toJSONString(supply));
    return BaseResult.success(map);
  }
  
  @RequestMapping("/deleteSupply")
  public @ResponseBody BaseResult deleteSupply(String supplyids){
    logger.info(String.format("delete %s supply", supplyids));
    BaseResult result = supplyManageService.deleteSupply(supplyids);
    logger.info("delete supply result ： "+result);
    return result;
  }
  
  @RequestMapping("/checkThroughSupply")
  public @ResponseBody BaseResult checkThroughSupply(String operator,String supplyids){
    String status = "2";
    logger.info(String.format("check through %s supply ", supplyids));
    BaseResult result = supplyManageService.checkSupply(status, supplyids,operator);
    logger.info("check through supply reuslt :"+result);
    return result;
  }
  
  @RequestMapping("/checkRefuseSupply/{id}")
  public @ResponseBody BaseResult checkRefuseSupply(String operator,@PathVariable("id") String id){
    String status = "3";
    logger.info(String.format("chech refuse %s supply", id));
    BaseResult result = supplyManageService.checkSupply(status, id,operator);
    logger.info("check refuse result : "+ result);
    return result;
  }
  
  @RequestMapping("/checkFrozenSupply/{id}")
  public @ResponseBody BaseResult checkFrozenSupply(String operator,@PathVariable("id") String id){
    String status = "4";
    logger.info(String.format("frozen supply %s ", id));
    BaseResult result = supplyManageService.checkSupply(status, id,operator);
    logger.info("frozen supply result : "+ id);
    return result;
  }
  
  @RequestMapping("/updateSupply")
  public @ResponseBody BaseResult updateSupply(SupplyVo supply){
    logger.info(String.format("update supply , params=[%s]", JSON.toJSONString(supply)));
    supply.setSupplyStatus("1");
    supplyManageService.updateSupply(supply);
    return BaseResult.success();
  }
  @RequestMapping("/supplyApproveList")
  public @ResponseBody BaseResult supplyApproveList(String supplyID,PaginationInfo paginationInfo){
    return supplyManageService.getSupplyApproveList(supplyID,paginationInfo);
  }
 
  @RequestMapping(value="/supplyAreaList",method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult supplyAreaList(SupplyConditionVo supplyConditionVo,PaginationInfo paginationInfo,HttpServletRequest req){
      logger.info(String.format("query supplyArealist,params:condition=[%s],page=[%s]", 
                      JSON.toJSONString(supplyConditionVo),JSON.toJSONString(paginationInfo)));
      String supplyName = req.getParameter("supplyName");
      BaseResult br = null;
      if("" != supplyName){
          supplyConditionVo.setSupplyName(supplyName);
      }
        br = supplyAreaService.selectBySupplyAreaForPage(supplyConditionVo, paginationInfo);
        logger.info(String.format("query supplyArealist result:%s", JSON.toJSONString(br)));
       return br;
  }
  @RequestMapping(value="/getAreaName",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult getAreaName(String areaId){
      logger.info("getAreaName check data begin "+JSON.toJSONString(areaId));
      BaseResult baseResult = supplyAreaService.getAreaName(areaId);
      return baseResult;
  }
 /* @SuppressWarnings("unchecked")*/
  @RequestMapping(value="getSupplyAreaById",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult getSupplyAreaById(String id,ModelMap map){
      logger.info("getSupplyAreaById begin");
      try {
          BaseResult bs = supplyAreaService.getSupplyAreaById(id);
          logger.info(String.format("query getSupplyAreaById result:%s", JSON.toJSONString(bs)));
          return BaseResult.success(bs);
          
    } catch (Exception e) {
        logger.error("SupplyManageRestController getSupplyAreaById error:" + e.getMessage());
        logger.error("SupplyManageRestController getSupplyAreaById end.");
        return BaseResult.exception(e.getMessage());
    }
      /*BaseResult bs = supplyAreaService.getSupplyAreaById(id);
      if(areaId != null){
          BaseResult baseResult = areaService.getByAreaId(areaId);
          AdminAreaVo adminAreaVo = (AdminAreaVo)baseResult.getData();
          String province = adminAreaVo.getProvince();
          //String county = adminAreaVo.getCounty();
          String city = adminAreaVo.getCity();
          List<AdminAreaVo> provinces =( List<AdminAreaVo>) areaService.getByParentId("1").getData();
          List<AdminAreaVo> citys = ( List<AdminAreaVo>) areaService.getByParentId(province).getData();
          List<AdminAreaVo> countrys = (List<AdminAreaVo>) areaService.getByParentId(city).getData();
          map.put("provinces", provinces);
          map.put("citys", citys);
          map.put("countrys", countrys);
          map.put("adminAreaVo", adminAreaVo);
          map.put("supplyArea", bs);
      }else{
          map.put("supplyArea", bs);
      }
      */
      //logger.info(String.format("query getSupplyAreaById result:%s", JSON.toJSONString(bs)));
      //return BaseResult.success(map);  
  }
  
  @RequestMapping(value="/getSupplyList",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult getSupplyList(HttpServletRequest request,HttpServletResponse response){
      logger.info("getSupplyList begin");
      BaseResult baseResult = supplyAreaService.getSupplyList();
      logger.info(JSON.toJSONString(baseResult));
      return baseResult;
  }
  @RequestMapping(value="/insertSupplyArea",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult insertSupplyArea(SupplyAreaVo supplyAreaVo){
      logger.info("insertSupplyArea begin");
      BaseResult baseResult = supplyAreaService.insertSupplyArea(supplyAreaVo);
      logger.info(JSON.toJSONString(baseResult));
      return BaseResult.success(baseResult);
  }
  @RequestMapping(value="/deleteSupplyArea",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult deleteSupplyArea(SupplyAreaVo supplyAreaVo){
      logger.info("deleteSupplyArea begin");
      BaseResult baseResult = supplyAreaService.deleteSupplyArea(supplyAreaVo);
      logger.info(JSON.toJSONString(baseResult));
      return BaseResult.success(baseResult);
  }
  @RequestMapping(value="/updateById",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult updateById(SupplyAreaVo supplyAreaVo){
      logger.info("updateById begin");
      BaseResult baseResult = supplyAreaService.updateById(supplyAreaVo);
     logger.info( JSON.toJSONString(baseResult));
      return BaseResult.success(baseResult);
  }
}
